package cn.ulc.socket.admin.service.site.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.site.SiteRegionDTO;
import cn.ulc.socket.admin.model.dto.site.SiteRegionListDTO;
import cn.ulc.socket.admin.model.param.site.SiteRegionParam;
import cn.ulc.socket.admin.service.site.SiteRegionServ;
import cn.ulc.store.dao.PubDataDao;
import cn.ulc.store.dao.site.SiteRegionDao;
import cn.ulc.store.dao.staff.StaffRoleDao;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.site.SiteRegion;
import cn.ulc.store.entity.staff.StaffRole;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
/**
 * 分站区域信息
 */
@Service
@AllArgsConstructor
public class SiteRegionServImpl extends BaseService implements SiteRegionServ {
    private static final Log log = LogFactory.get();

    SiteRegionDao siteRegionDao;
    PubDataDao pubDataDao;
    StaffRoleDao roleDao;
    @Override
    public SiteRegionListDTO findListPage(String key,Long siteId,PaginationParam param) {
        Company company = getCurrentCompany();
        Map<String,Object> condition = new HashMap<>();
        condition.put("siteId",siteId);
        condition.put("key",key);
        condition.put("companyId",company.getId());
        PageQuery pageQuery = initPageQuery(param);
        pageQuery.setParas(condition);
        siteRegionDao.findListPage(pageQuery);
        return new SiteRegionListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public SiteRegionDTO findDetail(Long id) {
        SiteRegion entity = siteRegionDao.findById(id);
        return new SiteRegionDTO(entity);
    }

    @Override
    public Boolean save(SiteRegionParam param) {
        Company company = getCurrentCompany();
        SiteRegion region = new SiteRegion();
        setBaseInfo(region);
        String code = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_ROLE,"骑手");
        //设置员工角色  骑手
        if(StringUtils.isNotBlank(code)){
            SiteRegion checkRegion = siteRegionDao.findByStaff(company.getId(),param.getStaffId());
            if(checkRegion!=null) throw new PlatformException("该员工已经是"+checkRegion.getName()+"区域的骑手");
            StaffRole staffRole = roleDao.findByRole(param.getStaffId(),code);
            if(staffRole == null){
                staffRole = new StaffRole();
                setBaseInfo(staffRole);
                staffRole.setStaffId(param.getStaffId());
                staffRole.setRole(code);
                staffRole.setRoleName("骑手");
                staffRole.setCompanyId(company.getId());
                roleDao.save(staffRole);
            }
        }
        region.setName(param.getName());
        region.setSiteId(param.getSiteId());
        region.setStaffId(param.getStaffId());
        region.setStatus(AvailableStatus.AVAILABLE);
        region.setRemark(param.getRemark());
        siteRegionDao.save(region);
        //其他关联处理
        return true;
    }

    @Override
    @Transactional
    public Boolean update(Long id, SiteRegionParam param) {
        Company company = getCurrentCompany();
        SiteRegion region = siteRegionDao.findOne(id);
        if(region==null) throw new PlatformException("获取区域信息失败!");
        if(region.getStaffId().intValue()!=param.getStaffId().intValue()){
            String code = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_ROLE,"骑手");
            if(StringUtils.isNotBlank(code)){
                SiteRegion checkRegion = siteRegionDao.findByStaff(company.getId(),param.getStaffId());
                if(checkRegion!=null) throw new PlatformException("该员工已经是"+checkRegion.getName()+"区域的骑手");
                StaffRole role = roleDao.findByRole(region.getStaffId(),code);
                if(role!=null){
                    setBaseInfo(role);
                    role.setDeleted(DeletedStatus.DELETED);
                    roleDao.save(role);
                }
            }
        }
        region.setName(param.getName());
        region.setStaffId(param.getStaffId());
        region.setRemark(param.getRemark());
        siteRegionDao.save(region);
        return true;
    }

    @Override
    public Long delete(Long id) {
        Company company = getCurrentCompany();
        SiteRegion region = siteRegionDao.findOne(id);
        if(region == null) throw new PlatformException("分站区域信息已不存在");
        String code = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_ROLE,"骑手");
        if(StringUtils.isNotBlank(code)){
            StaffRole staffRole = roleDao.findByRole(region.getStaffId(),code);
            if(staffRole!=null){
                setBaseInfo(staffRole);
                staffRole.setDeleted(DeletedStatus.DELETED);
                roleDao.save(staffRole);
            }
        }
        region.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(region);
        siteRegionDao.save(region);
        return id;
    }

    @Override
    public Boolean changeStatus(Long id, AvailableStatus status) {
        SiteRegion region = siteRegionDao.findOne(id);
        if(region == null) throw new PlatformException("分站区域信息已不存在");
        setBaseInfo(region);
        region.setStatus(status);
        siteRegionDao.save(region);
        return true;
    }
}
